package com.sukaiyi.generator;

import com.sukaiyi.generator.processor.SqlParser;
import com.sukaiyi.generator.processor.model.ClassInfo;
import freemarker.cache.StringTemplateLoader;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;

import java.io.IOException;
import java.io.Serializable;
import java.io.Writer;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class Test {

    public static void main(String[] args) throws IOException, TemplateException {
        String source = "package ${pkg}.entity;\n" +
                "\n" +
                "import com.baomidou.mybatisplus.annotation.TableField;\n" +
                "import com.baomidou.mybatisplus.annotation.TableName;\n" +
                "import lombok.Data;\n" +
                "import lombok.NoArgsConstructor;\n" +
                "\n" +
                "import java.io.Serializable;\n" +
                "\n" +
                "/**\n" +
                " * ${classComment}\n" +
                " * \n" +
                " * @author ${user}\n" +
                " * @date ${date?date}\n" +
                " */\n" +
                "@Data\n" +
                "@NoArgsConstructor\n" +
                "@TableName(\"${tableName}\")\n" +
                "public class ${entityName} implements Serializable {\n" +
                "<#list fields as field>\n" +
                "\t<#if field.fieldComment?? && field.fieldComment!=\"\">\n" +
                "\t/**\n" +
                "     * ${field.fieldComment}\n" +
                "     */\n" +
                "\t</#if>\n" +
                "    @TableField(\"${field.columnName}\")\n" +
                "    private ${field.fieldClass} ${field.fieldName};\n" +
                "</#list>\n" +
                "}";
        StringTemplateLoader loader = new StringTemplateLoader();
        loader.putTemplate("template", source);
        Configuration configuration = new Configuration(Configuration.VERSION_2_3_28);
        configuration.setTemplateLoader(loader);
        Template tpl = configuration.getTemplate("template", "utf-8");
        StringBuilderWriter writer = new StringBuilderWriter();

        String sql = "CREATE TABLE `cae_access` (\n" +
                "  `role_id` int(5) NOT NULL,\n" +
                "  `node_id` int(5) NOT NULL COMMENT '123',\n" +
                "  KEY `role_id` (`role_id`) USING BTREE,\n" +
                "  KEY `node_id` (`node_id`) USING BTREE\n" +
                ") ENGINE=InnoDB DEFAULT CHARSET=utf8";
        ClassInfo classInfo = SqlParser.processTableIntoClassInfo(sql);
        classInfo.setPkg("org.sss");
        Map<String, Object> dataModel = new HashMap<String, Object>(){{
            put("entityName", classInfo.getClassName());
            put("tableName", classInfo.getTableName());
            put("classComment", classInfo.getClassComment());
            put("fields", classInfo.getFieldList());
            put("pkg", classInfo.getPkg());
            put("user", "!23132");
            put("date", new Date());
        }};

        tpl.process(dataModel, writer);
        System.out.println(writer.toString());
    }

    public static class StringBuilderWriter extends Writer implements Serializable {
        private static final long serialVersionUID = -146927496096066153L;
        private final StringBuilder builder;

        public StringBuilderWriter() {
            this.builder = new StringBuilder();
        }

        public StringBuilderWriter(int capacity) {
            this.builder = new StringBuilder(capacity);
        }

        public StringBuilderWriter(StringBuilder builder) {
            this.builder = builder != null ? builder : new StringBuilder();
        }

        public Writer append(char value) {
            this.builder.append(value);
            return this;
        }

        public Writer append(CharSequence value) {
            this.builder.append(value);
            return this;
        }

        public Writer append(CharSequence value, int start, int end) {
            this.builder.append(value, start, end);
            return this;
        }

        public void close() {
        }

        public void flush() {
        }

        public void write(String value) {
            if (value != null) {
                this.builder.append(value);
            }

        }

        public void write(char[] value, int offset, int length) {
            if (value != null) {
                this.builder.append(value, offset, length);
            }

        }

        public StringBuilder getBuilder() {
            return this.builder;
        }

        public String toString() {
            return this.builder.toString();
        }
    }
}
